<?php
namespace Tbf\Bundle\UpyunBundle\Model\Driver;
class PhpDriver
{
    protected $upyun;
    public function __construct(\UpYun $upyun)
    {
        $this->upyun = $upyun;
    }
    public function Download($upyun_path,$local_path)
    {
    }
    /**
     * 递归下载文件
     * 非常慢...
     * @param string $upyun_path
     * @param string $file_path
     */
    public function DownloadDir($upyun_path,$file_path)
    {
        $data = $this->origin->getList($upyun_path);
        foreach ($data as $file) {
            $this_file_path = $file_path.'/'.$file['name'];
            $this_upyun_path = $upyun_path.'/'.$file['name'];
            if ($file['type']==='folder') {
                FileSystemFunction::mkdir($this_file_path);
                $this->DownloadDir($this_upyun_path,
                    $this_file_path);
            } elseif ($file['type']==='file') {
                $content = $this->origin->readFile($this_upyun_path);
                FileSystemFunction::putContent($this_file_path, $content);
                echo 'copy file'.$this_upyun_path."\n";
            } else {
                throw new UpyunException("unexpect file type:".$file['type']);
            }
        }
    }
    public function DownloadFile($upyun_path,$local_path)
    {
    }
    /**
     * 列表下载用php下载
     * @param array  $url_list
     * @param string $target_path
     */
    public function DownloadFileList(array $url_list,$target_path)
    {
        foreach ($url_list as $url) {
            $this_upyun_path = $url;
            $this_file_path = $target_path.'/'.$url;
            $content = $this->origin->readFile($this_upyun_path);
            FileSystemFunction::putContent($this_file_path, $content);
        }
    }
}
